﻿using Solidinfo.Common;
using Solidinfo.Model;
using Solidinfo.Web.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace Solidinfo.Web.Helpers
{
    public class ZRequiredAttribute : RequiredAttribute
    {
        public override string FormatErrorMessage(string name)
        {
            return base.FormatErrorMessage(name);
        }
    }
    public abstract class AllowEmptyValidateAttribute : ValidationAttribute
    {
        public bool AllowEmpty { get; set; }
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (AllowEmpty)
            {
                if (value == null)
                {
                    return ValidationResult.Success;
                }
                else
                {
                    var emailStr = value.ToString();
                    if (string.IsNullOrEmpty(emailStr))
                    {
                        return ValidationResult.Success;
                    }
                }
            }
            return SubIsValid(value, validationContext);
        }

        protected abstract ValidationResult SubIsValid(object value, ValidationContext validationContext);
    }
    /// <summary>
    /// 电子邮件验证
    /// </summary>
    public class EmailValidateAttribute : AllowEmptyValidateAttribute
    {
        protected override ValidationResult SubIsValid(object value, ValidationContext validationContext)
        {
            var emailStr = value as string;
            if (emailStr != null && SolidinfoRegex.CheckEmailByString(emailStr))
            {
                return ValidationResult.Success;
            }
            else
            {
                var dipslay = validationContext.DisplayName;
                return new ValidationResult(string.Format(MultilingualHelper.GetMultilingualString("Validation_Email"), dipslay));
            }

        }

    }


    public class ProvinceCityAttribute : ValidationAttribute
    {
        private readonly TenderInfoAreaTypeEnum _flag;
        public ProvinceCityAttribute(TenderInfoAreaTypeEnum flag)
        {
            _flag = flag;
        }
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (((TenderInfoViewModel)validationContext.ObjectInstance).B_TenderInfoAreaType == (int)_flag)
            {
                if (!string.IsNullOrEmpty(Convert.ToString(value)))
                {
                    return ValidationResult.Success;
                }
                else
                {
                    return new ValidationResult(string.Format(MultilingualHelper.GetMultilingualString("Filed_Isrequest"), validationContext.DisplayName));
                }
            }
            return ValidationResult.Success;
        }
    }



    //密码验证
    public class StringLengthCheckAttribute : AllowEmptyValidateAttribute
    {
        public StringLengthCheckAttribute(int minLength, int maxLength)
        {
            MinLength = minLength;
            MaxLength = maxLength;
        }
        public StringLengthCheckAttribute(int minLength)
            : this(minLength, 20)
        {

        }
        public StringLengthCheckAttribute()
            : this(6, 20)
        {
        }
        public int MinLength { get; set; }
        public int MaxLength { get; set; }
        protected override ValidationResult SubIsValid(object value, ValidationContext validationContext)
        {
            var passWordStr = value as string;
            if (passWordStr != null && passWordStr.Length >= MinLength && passWordStr.Length <= MaxLength)
            {
                return ValidationResult.Success;
            }
            else
            {
                var dipslay = validationContext.DisplayName;
                return new ValidationResult(string.Format(MultilingualHelper.GetMultilingualString("Validation_PassWord"), dipslay, MinLength, MaxLength));
            }
        }
    }
}